/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jbfilter.jsp.fcomps;

import javax.servlet.jsp.JspException;

import org.jbfilter.core.EnableStrategy;
import org.jbfilter.core.fcomps.single.SingleFilterComponent;
import org.jbfilter.jsp.PropertyPathHandler;

public abstract class SingleFilterComponentTag<E, T, V> extends FilterComponentTag<E> {

	private V value; // the real value or it's string representation
	private V valueCleanValue; // the real value or it's string representation
	private boolean valueSet;
	private boolean valueCleanValueSet;
	private String valueForNull = FilterComponentTag.BLANK;
	
	public void setValue(V value) {
		this.value = value;
		valueSet = true;
	}

	public void setValueForNull(String valueForNull) {
		this.valueForNull = valueForNull;
	}
	protected String getValueForNull() {
		return valueForNull;
	}
	public void setValueCleanValue(V valueCleanValue) {
		this.valueCleanValue = valueCleanValue;
		valueCleanValueSet = true;
	}

	@Override
	protected EnableStrategy getJspEnableStrategy() {
		return new EnableStrategy() {
			@Override
			public boolean decideEnabled() {
				SingleFilterComponent<E, T> sfComp = getSingleFilterComponent();
				T value = sfComp.getValue();
				if (value == null) {
					// disable component if value is null only if filter component
					// is NullTestingEnableable and null testing is enabled 
					if (isNullTestingCase()) {
						return true;
					} else {
						return false;
					}
				}
				return true;
			}
		};
	}

	@Override
	protected void setPropertiesFilterComponentTagCallback() throws JspException {
		PropertyPathHandler propertyPathHandler = getPropertyPathHandler();
		if (propertyPathHandler != null && propertyPathHandler.isPropertyPathChanged()) {
				SingleFilterComponent<E, T> sfComp = getSingleFilterComponent();
				// prevent illegal content because the nature (the T generic parameter) may change
				sfComp.setValue(null);
				sfComp.setValueCleanValue(null);
		}
		value = prepareValue(value);
		valueCleanValue = prepareValue(valueCleanValue);
		setValueProperty();
		setValueCleanValueProperty();
		setPropertiesSingleFilterComponentTagCallback();
	}

	private V prepareValue(V val) {
		return ValueForNullSupport.prepareValue(val, valueForNull);
	}
	
	abstract protected void setValueProperty() throws JspException;
	abstract protected void setValueCleanValueProperty() throws JspException;
	abstract protected void setPropertiesSingleFilterComponentTagCallback() throws JspException;
	
	@SuppressWarnings("unchecked")
	protected SingleFilterComponent<E, T> getSingleFilterComponent() {
		return (SingleFilterComponent<E, T>)getFilterComponent();
	}

	protected boolean isValueSet() {
		return valueSet;
	}
	protected boolean isValueCleanValueSet() {
		return valueCleanValueSet;
	}
	protected V getValue() {
		return value;
	}
	protected V getValueCleanValue() {
		return valueCleanValue;
	}
}
